package org.myblog.myblogserver.code.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.myblog.myblogserver.code.entity.Tabs;
import org.myblog.myblogserver.code.entity.User;
import org.myblog.myblogserver.code.service.TabsService;
import org.myblog.myblogserver.dao.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

@Api(tags = "Tabs")
@RestController
@RequestMapping("/tabs")
public class TabsController {

    @Autowired
    private TabsService tabsService;

    private Result  result = new Result();



    /**
     * post  /tabs/insert<br/>
     * json <br/>
     *
     * @author 智哥
     * @param tabs
     * @return Result
     * */
    @ApiOperation(value = "Tabs新增")
    @PostMapping("/insert")
    public Result insert(@RequestBody Tabs tabs) {
        tabs.setId(null);
        tabs.getTab_name();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tab_name",tabs.getTab_name());
        Tabs re_tabs =tabsService.getOne(queryWrapper);
        if(re_tabs != null){
            result.client_fail("标签已存在", null);
            return result;
        }
        if(tabsService.save(tabs)){
            result.success("添加成功", null);
        }
        else{
            result.client_fail("添加失败", null);
        }
        return result;
    }



    /**
     * post  /tabs/selecteq<br/>
     * json <br/>
     * 不给条件全查，eq查询<br/>
     *
     * @author 智哥
     * @param tabs
     * @return Result
     * */
    @ApiOperation(value = "Tabs条件查询")
    @PostMapping("/selecteq")
    public Result selecteq(@RequestBody Tabs tabs) {
        QueryWrapper<Tabs> queryWrapper = new QueryWrapper<>();
        if(tabs.getTab_name() != null) queryWrapper.like("tab_name", tabs.getTab_name());
        result.success("查询成功", tabsService.list(queryWrapper));
        return result;
    }


    /**
     * post  /tabs/deleteAlleq<br/>
     * json <br/>
     * 不给条件全删，eq删除<br/>
     *
     * @author 智哥
     * @param tabs
     * @return Result
     * */
    @ApiOperation(value = "Tabs条件删除")
    @PostMapping("/deleteAlleq")
    public Result deleteAlleq(@RequestBody Tabs tabs) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if(tabs.getTab_name() != null) queryWrapper.eq("tab_name", tabs.getTab_name());
        if(tabsService.remove(queryWrapper)){
            result.success("删除成功", true);
        }
        else{
            result.server_fail("删除失败", false);
        }
        return result;
    }
}